package com.zzjc.webauth.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzjc.util.UUIDUtils;
import com.zzjc.webauth.entity.User;
import com.zzjc.webauth.mapper.UserDao;
import com.zzjc.webauth.service.UserService;
import com.zzjc.webauth.utils.ApiResponse;
import com.zzjc.webauth.utils.DateUtils;
import com.zzjc.webauth.utils.PasswordUtils;
import com.zzjc.webauth.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public ApiResponse<?> register(User user) {
        User existingUser = userDao.getUserByPhone(user.getUserAcct());
        if (existingUser != null) {
            return ResponseUtil.error("用户已经存在，请重新输入");
        }
        user.setPassword(PasswordUtils.generatePassword(user.getPassword()));
        user.setCreateTime(DateUtils.getNowDate());
        user.setUserCode(UUIDUtils.getUUID());
        user.setIsDelete(0);
        int count = userDao.insertUser(user);
        if (0 == count) {
            return ResponseUtil.error(-1, "新增失败请重试");
        }
        return ResponseUtil.success("新增成功");
    }


    @Override
    @Transactional
    public ApiResponse deleteUser(List<String> userList) {
        for (String a : userList) {
            if ("1".equals(a)) {
                return ResponseUtil.error("admin不可删除");
            }
        }
        List<User> existingUser = userDao.getUserById(userList);
        if (existingUser.size() < 1) {
            return ResponseUtil.error("用户不存在");
        } else {
            userDao.deleteUser(userList);
            return ResponseUtil.success(userList + "删除成功");
        }

    }

    @Override
    public ApiResponse listUser(User user) {
        PageInfo<User> pageInfo = null;
        try {
            int pageNum = 1; // 默认值
            if (user.getPageNum() != null) {
                pageNum = user.getPageNum();
            }
            int pageSize = 1000; // 默认值
            if (user.getPageSize() != null) {
                pageSize = user.getPageSize();
            }
            PageHelper.startPage(pageNum, pageSize);
            List<User> userList = userDao.findAll(user);
            pageInfo = new PageInfo<>(userList);
            return ResponseUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.error("查询失败，请重试");
        }

    }

    @Override
    public ApiResponse updateUser(User user) {
        try{
        User existingUser = userDao.getUserByPhone(user.getUserAcct());
        if (existingUser == null) {
            return ResponseUtil.error("用户不存在，请重新输入");
        }else {
            user.setUpdateTime(DateUtils.getNowDate());
            userDao.updateUser(user);
            return ResponseUtil.success(user.getUserAcct()+"更新成功");
        }
    }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.error("更新失败，请重试");
        }
    }

}